package com.educate.service.impl;

import com.educate.common.lang.ProblemRedisNameConst;
import com.educate.dao.ProblemLikeDao;
import com.educate.pojo.ProblemLike;
import com.educate.pojo.UserProblemLike;
import com.educate.service.ProblemLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiongyu
 * @date 2021/12/12 17:38
 */

@Service
public class ProblemLikeImpl implements ProblemLikeService {

    @Autowired
    private ProblemLikeDao problemLikeDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public boolean deleteProblemLikeBash(List<Integer> userProblemLike) {
        return problemLikeDao.deleteProblemLikeBash(userProblemLike,Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString())) == userProblemLike.size();
    }

    @Override
    public boolean addProblemLikeBash(List<ProblemLike> userProblemLike) {
        return problemLikeDao.addProblemLikeBash(userProblemLike,Long.parseLong(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString())) == userProblemLike.size();
    }

    /**
     * 将用户点赞数量存入redis中
     * @param userProblemLikeList
     * @return
     */
    @Override
    public boolean updateProblemLikeNumber(List<UserProblemLike> userProblemLikeList) {
        for (UserProblemLike userProblemLike : userProblemLikeList) {
            redisTemplate.opsForZSet().incrementScore(ProblemRedisNameConst.HOTPROBLEM, userProblemLike.getProblemId(), userProblemLike.getLike());
            redisTemplate.opsForHash().increment(ProblemRedisNameConst.PROBLEMLIKENUMBER, String.valueOf(userProblemLike.getProblemId()), userProblemLike.getLike());
        }

        return true;
    }
}
